<template>
	<view>
		<!-- 添加表单 -->
		<u--form v-if="show==0" :model="d" :rules="rules" ref="uForm">
			<u-form-item label="设置密码" borderBottom ref="item1" prop="set_password">
				<u--input type="password" v-model="d.set_password" style="background-color: #ffffff;" border="none"
					placeholder="请输入密码"></u--input>
			</u-form-item>
			<u-form-item label="确认密码" borderBottom ref="item1" prop="conf_password">
				<u--input type="password" v-model="d.conf_password" style="background-color: #ffffff;" border="none"
					placeholder="请再次输入密码"></u--input>
			</u-form-item>
			<view class="from_tips_text">{{from_tips}}</view>
			<u-button text="提交信息" @click="add"></u-button>
		</u--form>
		<!-- 编辑表单 -->
		<u--form v-else-if="show==1" :model="d" :rules="rules" ref="uForm">
			<u-form-item label="输入密码" borderBottom ref="item1" prop="password">
				<u--input type="password" v-model="d.password" style="background-color: #ffffff;" border="none"
					placeholder="请输入密码"></u--input>
			</u-form-item>
			<view class="from_tips_text">{{from_tips}}</view>
			<u-button text="提交信息" @click="verification"></u-button>
		</u--form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: 1,
				id: -1,
				datas: {
					password: "123456"
				},
				d: {
					password: "",
					set_password: "",
					conf_password: "",
				},
				from_tips: "",
				rules: {
					password: {
						type: "string",
						min: 6,
						max: 12,
						required: true,
						message: '请输入6~12位密码',
						trigger: ['blur', 'change']
					},
					set_password: {
						type: "string",
						min: 6,
						max: 12,
						required: true,
						message: '请输入6~12位密码',
						trigger: ['blur', 'change']
					},
					conf_password: {
						type: "string",
						min: 6,
						max: 12,
						required: true,
						message: '请输入6~12位密码',
						trigger: ['blur', 'change']
					}
				}
			}
		},
		onLoad(res) {
			this.id = res.id
			this.api.getIndex({
				id: this.id
			}).then(res => {
				console.log(res.data)
				if (res.data.name == "") { //
					this.show = 0
				} else {
					this.show = 1
				}
			})
		},
		methods: {
			add() {
				if(this.d.set_password.length>=6 && this.d.set_password.length<=12){
					if (this.d.set_password != this.d.conf_password) {
						this.from_tips = "密码与确认密码不一致"
					} else {
						this.from_tips = ""
						this.datas.password = this.d.set_password
						uni.navigateTo({
							url: "./add?id=" + this.id + "&status=-1&modify=1&password=" + this.datas.password
						})
					}
				}
			},
			verification() {
				this.api.authKey({
					id: this.id,
					key: this.d.password
				}).then(res => {
					if (res.data.status == 1) {
						this.from_tips = ""
						uni.navigateTo({
							url: "./add?id=" + this.id + "&modify=0"
						})
					} else {
						this.d.password = ""
						this.from_tips = res.data.msg
					}
				})
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules)
		}
	}
</script>

<style lang="scss">
	page {
		display: flex;
		align-items: center;
		justify-content: center;
		height: 100%;

		.from_tips_text {
			color: rgba(255, 0, 0, 0.5);
			text-align: center;
			margin-top: 20rpx;
			margin-bottom: 20rpx;
		}
	}
</style>